package com.ccjt.efcfs.cal;


import com.ccjt.efcfs.cal.interest.ActualInterestCal;
import com.ccjt.efcfs.cal.interest.FinanceInterestCal;
import com.ccjt.efcfs.cal.interest.MonthInterestCal;
import com.ccjt.efcfs.cal.interest.NatureInterestCal;
import com.ccjt.efcfs.dto.LoanInfo;
import com.ccjt.efcfs.dto.RepayPlanCal;
import com.ccjt.efcfs.util.DecimalUtils;

import java.math.BigDecimal;

import static com.ccjt.efcfs.constant.CalculateConstant.Decimal.BD_ONE;

/**
 * 等本等息
 * 概念：等本等息的意思是，每月的本金相等，利息也相等
 * 公式：每月偿还本息=贷款本金×(1＋月利率)÷还款期数
 */
public class AverageCapitalAverageInterestCalculate extends CalculateBase {
    public AverageCapitalAverageInterestCalculate(LoanInfo loan) {
        super(loan);
    }

    @Override
    protected void calRepayInterest(RepayPlanCal plan){
        // 6.应付利息
        // 6.1 本期计划周期应付利息
        plan.setMonthPerRepayInterest(loan.getAvgInterest());
        // 6.2 本期计划周期金融日应付利息
        plan.setFinancePerRepayInterest(loan.getAvgInterest());
        // 6.3 本期周期自然日应付利息
        plan.setNaturePerRepayInterest(loan.getAvgInterest());
        // 6.4 本期实际自然日应付利息
        plan.setActualPerRepayInterest(new ActualInterestCal().calPeriodInterest(plan));
    }

    @Override
    protected void calRepayPrincipal(RepayPlanCal plan) {
        // 7. 应付本金
        // 7.1 本期计划周期应付本金
        plan.setMonthPerRepayPrincipal(loan.getAvgPrincipal());
        // 7.2 本期计划周期金融日应付本金
        plan.setFinancePerRepayPrincipal(loan.getAvgPrincipal());
        // 7.3 本期周期自然日应付本金
        plan.setNaturePerRepayPrincipal(loan.getAvgPrincipal());
        // 7.4 本期实际自然日应付本金
        if (loan.getEarlyRepayTotal() != null && loan.getEarlyRepayDate() != null && plan.getBeginDate().before(loan.getEarlyRepayDate()) && loan.getEarlyRepayDate().before(plan.getEndDate())) {
            plan.setActualPerRepayPrincipal(DecimalUtils.sub(loan.getEarlyRepayTotal(), plan.getActualPerRepayInterest()));
            plan.setPlanPeriods(plan.getPlanPeriods().add(BD_ONE));
        } else {
            plan.setActualPerRepayPrincipal(loan.getAvgPrincipal());
        }
    }
}
